<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<style>
    .costPswhCombobox-title-info{
        background:#FFFEE6;
        color:#8F5700;
        padding:12px;
    }
</style>
<div style="text-align: center">
    <div class="costPswhCombobox-title-info">
        <div class="demo-tip icon-tip"></div>
        <div>
            标准工时类型:
            <input id="costPswhCombobox" class="easyui-combobox" name="dept" data-options="valueField:'id',textField:'text'" />
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="costPswhDatagridInstert()">增加根目录</a>
        </div>
    </div>
    <div class="costPswhDatagrid-div" style="width: 100%;margin: 0 auto">
<!--  工时数据表格-->
        <table id="costPswhDatagrid" style="width:100%;display: none">
            <thead>
            <tr>
                <th data-options="field:'productName',align:'left',editor:{type:'textarea'}" width="35%"  >名称</th>
                <th data-options="field:'logogram',align:'center',editor:{type:'textarea'}" width="10%">名称简写</th>
                <th data-options="field:'num',align:'center',editor:{type:'textarea'}" width="5%">序号</th>
                <th data-options="field:'slManHour',align:'center',editor:{type:'textarea'}" width="5%">工时</th>
                <th data-options="field:'actualValue',align:'center',editor:{type:'textarea'}" width="5%">灌装重量</th>
                <th data-options="field:'remark',align:'center',editor:{type:'textarea'}" width="30%">备注</th>
                <th data-options="field:'caozuo',align:'center'" width="10%" formatter="costPswhDatagridCaozuoFormatter">操作</th>
            </tr>
            </thead>
        </table>
    </div>
    <!-- 插入操作框 -->
    <div id="costPswhInsertDialog" style="display: none;text-align: center">

    </div>
</div>

<script th:inline="none">
    var costPswhDatagridId;//保存id值
$("#costPswhCombobox").combobox({
    panelHeight:"auto",
    data:[
        {id:'1',text:"水料标准工时"},
        {id:'2',text:"灌装基础工时"},
        {id:'3',text:"工艺标准工时"}
    ],
    onChange:function (newValue,oldValue) {
        //获取类型数据
        $("#costPswhDatagrid").treegrid({
            nowrap:false,
            idField:"id",
            treeField:'productName',
            url: "/costReportForm/costReportInitialInfoLayui/?type="+newValue,
            onDblClickRow:function (row) {//双击事件
                costPswhDatagridId=row.id
               $(this).treegrid('beginEdit', costPswhDatagridId);
            },
            onAfterEdit:function (row,changes) { //点击保存触发编辑完成事件
                row.subId=row.id;
                   var data= commonAjax("post","/costReportForm/costPswhUpdate",row)
                    windowsOperation(null,null,data)
                    //刷新表格
                    $("#costPswhDatagrid").treegrid("load",{type:row.initialType});
            }
        })
    }
})

//点击插入
function costPswhDatagridInstert(a){
    //若是Undefined证明要添加根目录
    var id,initialType;
    if(a){
        //获取要往此id下插入数据
        id=a.getAttribute("subId")//获取本id
        initialType=a.getAttribute("initialType")//获取当前类型
    }else{
        id=0;
        initialType=$("#costPswhCombobox").combobox("getValue");
        if(!initialType){
            windowsOperation(null,null,{message:"请先选择工时类型！"});
            return
        }

    }

     //弹出框
    $("#costPswhInsertDialog").dialog({
        title:"工时插入",
        width:500,
        height:300,
        content:' <form id="costPswhInsertForm">\n' +
            '            <!-- 父类id-->\n' +
            '            <input name="superId" style="display: none">\n' +
            '            <!-- 类型-->\n' +
            '            <input  name="initialType" style="display: none">\n' +
            '            <span style="color: #8F5700;font-size: 10px">---------------若是创建目录操作只需填写名称一栏即可--------------</span>\n' +
            '            <div style="margin-top: 10px">\n' +
            '                <input id="costPswhInsertFormNum" class="easyui-textbox"style="width: 28%" data-options="label:\'序号\',labelAlign:\'right\',labelWidth:40,prompt:\'\'" name="num">\n' +
            '                <input id="costPswhInsertFormSlManHour" class="easyui-textbox"style="width: 28%" data-options="label:\'工时\',labelAlign:\'right\',labelWidth:40,prompt:\'\'" name="slManHour">\n' +
            '                <input id="costPswhInsertFormSlManHour" class="easyui-textbox"style="width: 33%" data-options="label:\'灌装重量\',labelAlign:\'right\',labelWidth:60,prompt:\'极少工时需要\'" name="actualValue">\n' +
            '            </div>\n' +
            '            <div style="margin-top: 10px">\n' +
            '                <input class="easyui-textbox" style="width: 90%" data-options="label:\'名称\',labelAlign:\'right\',labelWidth:40" name="productName">\n' +
            '            </div>\n' +
            '            <div style="margin-top: 10px">\n' +
            '                <input class="easyui-textbox"style="width: 90%" data-options="label:\'简写\',labelAlign:\'right\',labelWidth:40" name="logogram">\n' +
            '            </div>\n' +
            '            <div style="margin-top: 10px">\n' +
            '            <input class="easyui-textbox"style="width: 90%;height: 60px" data-options="label:\'备注\',labelAlign:\'right\',labelWidth:40,multiline:true" name="remark">\n' +
            '            </div>\n' +
            '        </form>',
        onOpen:function(){
            //往form中赋值
            $("input[name=superId]").val(id)
            $("input[name=initialType]").val(initialType)
        },
        buttons:[{
            text:'保存',
            handler:function(){
                //添加文件夹不需要序号和工时:需要赋值默认值
                var num = $("#costPswhInsertFormNum").textbox("getValue")
                var slManHour = $("#costPswhInsertFormSlManHour").textbox("getValue")

                if(!num){
                    $("#costPswhInsertFormNum").textbox("setValue",-1)  //后台有判断
                }
                if(!slManHour){
                    $("#costPswhInsertFormSlManHour").textbox("setValue",0) //后台有判断工时为0设置为null
                }
                var data = $("#costPswhInsertForm").serialize();
                var rData= commonAjax("post","/costReportForm/costPswhAdd",data)
                if(rData.statu==200){
                    //刷新表格
                    $("#costPswhDatagrid").treegrid("reload",{type:initialType});
                    //关闭
                    $("#costPswhInsertDialog").dialog("close");
                }
                windowsOperation(null,null,rData)
            }
        }]

    })

}

//点击保存--（触发编辑完成事件）
function costPswhDatagridSave(){
    if (costPswhDatagridId != undefined){
        var t = $('#costPswhDatagrid');
        t.treegrid('endEdit', costPswhDatagridId);
        costPswhDatagridId = undefined;
    }
}

//点击删除
function costPswhDatagridDelete(a){
    var id=a.getAttribute("subId")//获取本id
    var initialType=a.getAttribute("initialType")//获取当前类型
    $.messager.confirm('确认对话框', '警告！若删除的是<span style="color: red">目录</span>会删除目录下的所有哦！', function(r){
        if (r){
            var data= commonAjax("post","/costReportForm/costPswhDel",{'id':id})
            if(data.statu==200){
                //刷新表格
                $("#costPswhDatagrid").treegrid("load",{type:initialType});
            }
            windowsOperation(null,null,data)
        }
    });

}
//操作格式化
function costPswhDatagridCaozuoFormatter(value,row,index) {
     var content='<a href="javascript:void(0)" subId="'+row.id+'" initialType="'+row.initialType+'" onclick="costPswhDatagridInstert(this)">插入</a>&nbsp&nbsp'+
        '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="costPswhDatagridSave()">保存</a>&nbsp&nbsp'+
        '<a href="javascript:void(0)" subId="'+row.id+'" initialType="'+row.initialType+'" class="easyui-linkbutton" onclick="costPswhDatagridDelete(this)">删除</a>';
     return content;
}    


</script>

</body>
</html>